package app.pub.audioPlayer;

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class MyPlayer {

	public String fileName = null;
	
	public MyPlayer() {
		
	}

	public void play() throws Exception {
		File file = new File(fileName);
		AudioInputStream stream = AudioSystem.getAudioInputStream(file);
		AudioFormat format = stream.getFormat();
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
		byte[] buf = new byte[512 * 1024];
		line.open();
		line.start();
		int nbytes = 0;
		while (nbytes != -1) {
			nbytes = stream.read(buf, 0, buf.length);
			if (nbytes >= 0){
				line.write(buf, 0, nbytes);
			}
		}
		line.drain();
		line.close();
		System.out.println("play over.");
	}

	public static void main(String[] args) throws Exception {
		MyPlayer player = new MyPlayer();
		player.play();
	}

	

}